package com.gcxy.conf.exception.handler;

import com.gcxy.conf.exception.annotation.GlobalException;
import com.gcxy.conf.exception.exception.IGlobalException;
import com.gcxy.conf.result.R;
import com.gcxy.conf.result.ResultCodeEnum;
import org.springframework.http.converter.HttpMessageNotReadableException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Http消息不可读异常
 * HttpMessageNotReadableException：通常是controller层传入参数时造成的不可解析的异常
 *
 * @author shineTan
 * @date 2022/12/26 14:46
 */
@GlobalException(throwable = HttpMessageNotReadableException.class)
public class HttpMessageNotReadableExceptionHandler implements IGlobalException<R, HttpMessageNotReadableException> {

    @Override
    public R exception(HttpMessageNotReadableException throwable, HttpServletRequest request, HttpServletResponse response) {
        return BaseThrowExceptionHandler.exception(throwable, ResultCodeEnum.REQUEST_PARAM_ERROR, () -> {
                    String msg = "";
                    if (null == throwable.getCause()) {
                        msg = "请求参数为空";
                    } else {
                        msg = throwable.getMessage();
                    }
                    return msg;
                }
        );
    }
}
